package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.vo.request.AssessmentSearchVo;
import com.zhijian.medical.entity.vo.request.AssessmentStartVo;
import com.zhijian.medical.entity.vo.request.AssessmentSubmitVo;
import com.zhijian.medical.entity.vo.response.AssessmentDoingVo;
import com.zhijian.medical.entity.vo.response.AssessmentDoneVo;
import com.zhijian.medical.entity.vo.response.ExamResultVo;
import com.zhijian.medical.entity.vo.response.MyAssessmentVo;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.MyAssessmentService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;

/**
 * 我的考核【新】
 *
 * @author HCG
 * @version 1.0
 * @date 2023-02-16
 */
@Slf4j
@Controller
@Api(tags = "我的考核【新】")
public class MyAssessmentController {

    @Autowired
    private MyAssessmentService myAssessmentService;

    /**
     * 开始考核
     *
     * @return
     */
    @Response
    @PostMapping("/assessment/start")
    @ApiOperation("开始考核")
    public AssessmentDoingVo start(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody AssessmentStartVo param) {
        param.setUserId(personDto.getId());
        param.setUserName(personDto.getName());
        return myAssessmentService.start(param);
    }

    /**
     * 某个做卷人的试卷详情
     *
     * @return
     */
    @Response
    @GetMapping("/assessment/done/detail/{assessmentId}")
    @ApiOperation("某个做卷人的试卷详情")
    public AssessmentDoneVo doneDetail(@ApiParam(name = "assessmentId", value = "考核id") @PathVariable("assessmentId") Long assessmentId,
                                       @ApiParam(name = "personId", value = "做卷人id，我的考核里面可不传", required = false) @RequestParam(value = "personId", required = false) Long personId) {
        //todo
        return myAssessmentService.doneDetail(assessmentId, Objects.nonNull(personId) ? personId : CurrentUserHolder.getUser().getId());
    }

    /**
     * 提交试卷，or中途保存
     *
     * @return
     */
    @Response
    @PostMapping("/assessment/paper/submit")
    @ApiOperation("提交试卷，or中途保存")
    public ExamResultVo submit(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody AssessmentSubmitVo examSubmitVo) {
        //todo
        examSubmitVo.setUserId(personDto.getId());
        examSubmitVo.setUserName(personDto.getName());
        log.info("assessment submit, the param is {}", examSubmitVo);
        return myAssessmentService.submit(examSubmitVo);
    }

    /**
     * 我的考核分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/assessment/my/page")
    @ApiOperation("我的考核分页查询")
    public Page<MyAssessmentVo> selectMyAssessment(@RequestBody(required = false) AssessmentSearchVo param) {
        if (param == null)
            param = AssessmentSearchVo.builder().build();
        param.setDoPersonId(CurrentUserHolder.getUser().getId());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return myAssessmentService.selectMyPage(param);
    }

//    @Response
//    @PostMapping("/assessment/fix")
//    @ApiOperation("fix")
//    public boolean fix(){
//        return myAssessmentService.fix(44L);
//    }
}
